﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
using System.Data;
using System.Data.SqlClient;
using WP09S03_OfficeTracking.DAL;

namespace WP09S03_OfficeTracking.Controller
{
    class ItemController : ParentClass.ParentController 
    {

        private DAL.DSItem DS;
      

        public ItemController():base()
    {
        DS = new DSItem();
           
       
    }
        public DAL.DSItem.CategoryDataTable getCategories()
        { 
        
      DAL.DSItemTableAdapters.CategoryTableAdapter da =new DAL.DSItemTableAdapters.CategoryTableAdapter();

             return da.GetData();

        }

        public DAL.DSItem.CategoryDataTable getCategories(bool Consumable)
        {

            DAL.DSItemTableAdapters.CategoryTableAdapter da = new DAL.DSItemTableAdapters.CategoryTableAdapter();

            return da.GetDataByConsumable(Convert.ToInt32(Consumable));

        }

        public bool insertItem(int intCategoryID,string strItemName,string strBrand,string strModel,string strItemDesc,int intQuantity,int intThreshold,decimal decPrice, bool bolConsumable) {

            DAL.DSItemTableAdapters.OfficeItemsTableAdapter  da = new DAL.DSItemTableAdapters.OfficeItemsTableAdapter();

            int ret=da.Insert("0000000000", intCategoryID, strItemName, strBrand, strModel, strItemDesc, intQuantity, intThreshold, decPrice, bolConsumable, DateTime.Now, null);

            return (ret > 0);


        }

        public bool InsertCategory(ref int intCategoryID,string strCategoryName,bool blConsumable) {
            DAL.DSItemTableAdapters.CategoryTableAdapter da = new DAL.DSItemTableAdapters.CategoryTableAdapter();

            int ret=-1;

            try
            {
                ret = da.Insert(strCategoryName, blConsumable);

                if (ret == 0) { 
                //command run successfully, but not record affacted, then "duplicated category ID"
                    throw new Exception("Add Category Failed!");
                }
            }
            catch(Exception ex) {
                FillError(ex);
                throw ex;
           
            }

            return Convert.ToBoolean(ret);

        }

        public bool UpdateCategory(DSItem.CategoryDataTable dataTable) {


            DAL.DSItemTableAdapters.CategoryTableAdapter da = new DAL.DSItemTableAdapters.CategoryTableAdapter();

            try
            {
                da.Update(dataTable);
                dataTable.AcceptChanges();

                return true;

            }
            catch (Exception ex)
            {
                FillError(ex);
                throw ex;

            }
            

        
        }


        public int CheckCategoryItem(int intCategoryID) {
           SqlParameter[] para=new SqlParameter[]{ new SqlParameter("@CategoryID",SqlDbType.Int  ,4)};

            para[0].Value =intCategoryID;

    
            Object ret;

            ret = SqlHelper.ExecuteScalar(base.strConnection, CommandType.StoredProcedure, "sp_CategoryCheckDelete", para);


            if (ret == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(ret);
            }

        }

        public int CheckItemID(string itemID)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@ItemID", SqlDbType.Char , 10) };

            para[0].Value = itemID;


            Object ret;

            ret = SqlHelper.ExecuteScalar(base.strConnection, CommandType.StoredProcedure, "sp_ItemCheckDelete", para);


            if (ret == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(ret);
            }

        }


        public int CheckItemDetailID(string itemDetailID)
        {
            SqlParameter[] para = new SqlParameter[] { new SqlParameter("@ItemDetailID", SqlDbType.Char, 12) };

            para[0].Value = itemDetailID;


            Object ret;

            ret = SqlHelper.ExecuteScalar(base.strConnection, CommandType.StoredProcedure, "sp_ItemDetailCheckDelete", para);


            if (ret == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(ret);
            }

        }

        public int GetItemCountReplenish()
        {
            SqlParameter[] para = new SqlParameter[]{} ;

            
            Object ret;

            ret = SqlHelper.ExecuteScalar(base.strConnection, CommandType.StoredProcedure, "sp_ItemCountReplenish", para);


            if (ret == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(ret);
            }

        }



        public int GetItemCountDueReturn()
        {
            SqlParameter[] para = new SqlParameter[] { };

//TODO
            Object ret;

            ret = SqlHelper.ExecuteScalar(base.strConnection, CommandType.StoredProcedure, "sp_ItemCountDueReturn", para);


            if (ret == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(ret);
            }

        }

        public DSItem.DeviceStatusDataTable GetDeviceStatus()
        {
            DAL.DSItemTableAdapters.DeviceStatusTableAdapter adapter = new WP09S03_OfficeTracking.DAL.DSItemTableAdapters.DeviceStatusTableAdapter();
            return adapter.GetData();
        }


    }
}

